package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.dto.AthleteNameDateDto;
import com.ruoyi.system.domain.pojo.Score;
import com.ruoyi.system.domain.pojo.ScoreEachDay;
import com.ruoyi.system.domain.vo.AthleteScoreVo2;
import com.ruoyi.system.mapper.AthleteScoreMapper;
import com.ruoyi.system.service.AthleteScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AthleteScoreServiceImpl implements AthleteScoreService {

    @Autowired
    private AthleteScoreMapper athleteScoreMapper;

    public List<AthleteScoreVo2> getAthleteScore(AthleteNameDateDto athleteNameDateDto){

        List<String> nameList = athleteNameDateDto.getNameList();
        LocalDate startTime = athleteNameDateDto.getStartTime();
        LocalDate endTime = athleteNameDateDto.getEndTime();

        //开始时间的零点零分零秒
        //结束时间的23时59分59秒
        LocalDateTime st = startTime.atStartOfDay();
        LocalDateTime et = endTime.atTime(23, 59, 59);

        //转为date类型
        Date startDate = Date.from(st.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(et.atZone(ZoneId.systemDefault()).toInstant());

        List<Score> scores = athleteScoreMapper.getAthleteScore(nameList, startDate.getTime()/1000, endDate.getTime()/1000);
        if (scores == null || scores.size() == 0){
            throw new RuntimeException("数据不存在");
        }


        //List<AthleteScoreVo> scoreVos = new ArrayList<>();
        List<AthleteScoreVo2> athleteScoreVo2s = new ArrayList<>();

        for (String s : nameList) {
            //存储每个人的成绩
            AthleteScoreVo2 athleteScoreVo2 = new AthleteScoreVo2();
            athleteScoreVo2.setName(s);

            List<ScoreEachDay> scoreEachDays = new ArrayList<>();

            //将每个人查出的数据根据时间进行排序
            List<Score> newScores = scores.stream()
                    .filter(score -> score.getAthleteName().equals(s))
                    .sorted(Comparator.comparing(Score::getDateTime))
                    .map(score -> {score.setDateTime(score.getDateTime().substring(0, 10));
                        return score;
                    })
                    .collect(Collectors.toList());

            //将每天的成绩分开存储
            List<String> timeList = newScores.stream().map(Score::getDateTime).distinct().collect(Collectors.toList());
            for (String time : timeList) {
                ScoreEachDay scoreEachDay = new ScoreEachDay();
                scoreEachDay.setDatetime(time);
                scoreEachDay.setSocreList(newScores.stream().filter(score -> score.getDateTime().equals(time)).map(Score::getScores).collect(Collectors.toList()));
                scoreEachDays.add(scoreEachDay);
            }

            athleteScoreVo2.setAthleteScores(scoreEachDays);
            athleteScoreVo2s.add(athleteScoreVo2);
        }

        return athleteScoreVo2s;

    }

}
